﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace MassIntellect.WorkIt.Design
{
    public partial class frmCollectionEditor<T> : Form
        where T:class
    {
        public frmCollectionEditor(IEnumerable<T> coll)
        {
            InitializeComponent();
            this.Collection = coll ?? new List<T>();
        }

        public IEnumerable<T> Collection
        {
            get { return _Collection; }
            set
            {
                SetCollection(value);
            }
        }
        private List<T> _Collection;

        private void SetCollection(IEnumerable<T> value)
        {
            _Collection = value.ToList();
            if (_Collection.Count > 0)
            {
                foreach (var item in _Collection)
                    lbItems.Items.Add(item);
                lbItems.SelectedIndex = 0;
            }
        }

        public string Title
        {
            get
            {
                return this.Text;
            }
            set
            {
                this.Text = value;
            }
        }

        private void bUp_Click(object sender, EventArgs e)
        {

        }

        private void bDown_Click(object sender, EventArgs e)
        {

        }

        private void bAdd_Click(object sender, EventArgs e)
        {

            _Collection.Add(typeof(T).CreateInstance<T>());
        }

        private void bRemove_Click(object sender, EventArgs e)
        {
            if (lbItems.SelectedIndex != -1)
            {
                lbItems.Items.RemoveAt(lbItems.SelectedIndex);
                _Collection.Remove((T)lbItems.SelectedItem);                
            }
        }

        private void bOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void bCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void lbItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbItems.SelectedIndex != -1)
                pg.SelectedObject = lbItems.SelectedItem;
        }

        private void frmCollectionEditor_Load(object sender, EventArgs e)
        {
            DisplayNameAttribute da;
            if (types.Length > 0)
            {
                foreach (var item in types)
                {
                    cbType.Items.Add(new TypeReference
                    {
                        Type = item,
                        Name = ((da = item.GetAttribute<DisplayNameAttribute>(true)) != null)
                                        ? da.DisplayName
                                        : item.Name
                    });
                }
                cbType.SelectedIndex = 0;
            }
            
        }

        private static Type[] types = new Type[0];

        public static void UpdateTypes()
        {
            types = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(s => s.GetTypes())
                .Where(s =>
                    !s.IsAbstract && !s.IsGenericType && typeof(T).IsAssignableFrom(s) && s.GetConstructor(Type.EmptyTypes) != null)
                .ToArray();
        }

        static frmCollectionEditor()
        {
            UpdateTypes();
        }
    }

    internal class TypeReference
    {
        public string Name;

        public Type Type;

        public override string ToString()
        {
            return Name;
        }
    }
}
